var KEY_PAGE_UP = 33;
var KEY_PAGE_DOWN = 34;
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_ENTER = 13;
var KEY_TAB = 9;
function isUpDownKey(keyCode){return keyCode == KEY_PAGE_UP || keyCode == KEY_PAGE_DOWN || keyCode == KEY_DOWN || keyCode == KEY_UP;}
function isEnterKey(keyCode){return keyCode == KEY_ENTER || keyCode == KEY_TAB}

function showSel(el,show){// show/hide dealers select box
	//show = show && _sel().length > 0;
	//if( !show && ((event||{}).toElement||{}).dealerUI == 1 )return;
	//_sel().style.display = show ? "" : "none";
	_sel().style.posWidth = el.offsetWidth;
	_sel().style.top = getTopPos(el)+el.offsetHeight;
	_sel().style.left = getLeftPos(el);
}
function doTxt_A(el){//dealer change handler
	if( event && event.propertyName != "value" || !el.keyPressed )return;
	clearTimeout(doTxt.proc);
	doTxt.proc = setTimeout(function(){doTxt(el)},100);
}
function doTxt(el){//dealer change handler
	el.keyPressed = false;
	_sel().selectedIndex = -1;
	var value = _dealerName(el);
	if( value.length < 2 )return _txtDealersFilter("");
	loadDealers(el);
}
function doSel_A(){
	clearTimeout(doSel_A.proc);
	doSel_A.proc = setTimeout("doSelDealer()",100);
}
function doSelDealer(){// "selected dealer changed" handler
	if( _selText() != null )txtDealer.value = _selText();
	return;
	if( _sel().selectedIndex >= 0 )return alert("Save dealer :"+_sel().value);
}
function doNavigation(el){
	switch(event.keyCode){
		case 40://Down
			_selIndex('d');
			break;
		case 38://Up
			_selIndex( _selIndex()<0?_sel().length-1 : _selIndex('u'));
			break;
		case 33://Page UP
			slideSelectIndex(_sel(),-_selSize()+1);
			break;
		case 34://Page DOWN
			slideSelectIndex(_sel(),_selSize()-1);
			break;
	}
	// Local
	function slideSelectIndex(sel,directon){
		for( var step=directon>0?-1:1; directon != 0; directon += step )
			if( sel.selectedIndex == 0 && directon < 0 || sel.selectedIndex == sel.length-1 && directon > 0 )break;
			else sel.selectedIndex -= step;
	}
}
function setSelValueWhitNoEvent(sel,value){
	var foo = sel.onchange;
	sel.onchange = null;
	setElementValue(sel,value);
	sel.onchange = foo;
}
function setSelIndexWhitNoEvent(sel,index){
	var foo = sel.onchange;
	sel.onchange = null;
	sel.selectedIndex = index;;
	sel.onchange = foo;
}
function loadDealers(el){
	if( !WS )return setTimeout(function(){loadDealers(el)},1000);
	if( _txtDealersFilter() && _txtDealersFilter().length <= el.value.length ){
		//debugger;
		return cb(null,el.value);
	}
	WS(cb,"GuessDealer",_dealerName(el),false);
	el.filter = el.value;
	_txtDealersFilter()
	///Locals
	function cb(ret,filter){
		if( ret){
			if( showSOAPError(ret) )return;
			if( ret.value.length < 10 )return _txtDealersFilter("");
			xmlDealers.loadXML(ret.value);
		}
		fillDealersSelect();
	}
}
function fillDealersSelect(){
	var active = _chkActive().checked;
	var filter = _txtDealers().value.replace(/\(.*$/g,"");
	var sel = _sel();
	var rs = _rsDealers();
	if( !rs.state || rs.recordCount == 0 )return;
	sel.length = 0;
	var re = new RegExp(filter,"i");
	for( rs.MoveFirst() ; !rs.EOF && rs.Fields.Count > 1; rs.MoveNext() ){
		var isActive = eval(rs("Active").value);
		if( (!filter || re.exec(rs(0).value)) && (!active || isActive) ){
			sel[sel.length] = new Option(rs(0).value,rs(1).value);
			sel[sel.length-1].style.color = isActive ? "DARKGREEN" : "DARKRED";
		}
	}
	sel.size = Math.max(2,Math.min(sel.length,8));
	//debugger;
	_selIndex(0,true);
	//showDealers(el,true);
}
// ***** Helpers *****
function _dealerName(el){
	var s = escape(el.value.replace(/\s\(.{0,2}.*?$/,"").replace(/^\s+/,"").replace(/\s{2,}/g," "));
	//window.status = "["+s+"]";
	return s;
}
function _activeElement(){try{return document.activeElement}catch(e){return {}}}
function _chkActive(){return _chkActive.chk || (_chkActive.chk=getChildByAttr(document.body,"activedealer"))}
function _txtDealers(){return _txtDealers.txt || (_txtDealers.txt=getChildByAttr(document.body,"dealertextbox"))}
function _txtDealersFilter(filter){
	if( arguments.length > 0 )_txtDealers().filter = filter;
	return _txtDealers().filter
}
function _sel(){return _sel.sel || (_sel.sel=getChildByAttr(document.body,"flyingselect"))}
function _selIndex(index,noEvent){
	if( typeof(index)!='undefined' && isNaN(index) )return _selIndex(_selIndex()+(index=='u'?-1:1),noEvent);
	var sel = _sel();
	if( arguments.length > 0 && sel.length > 0 ){
		var foo = sel.onchange;
		if( noEvent )sel.onchange = null;
		sel.selectedIndex = index;;
		sel.onchange = foo;
	}
	return sel.selectedIndex;
}
function _selValue(){return _sel().value}
function _selSize(){return _sel().size}
function _selText(){return _selIndex()>=0?_sel()[_selIndex()].text:null}
function _dealerID(){return _sel().selectedIndex < 0 ? -1 : _sel().value}
function _rsDealers(){return xmlDealers.recordset}
